﻿using System;
using System.Text;

namespace T.FAS.Runtime.DataAccess.CDOEngine
{
    /// <summary>
    /// 过滤条件语句类
    /// </summary>
    /// <remarks>过滤条件语句类</remarks>
    class FilterConditionSpecifySubClassStament : FilterConditionStatement
    {
        private SqlBuildingContext context;
        private bool IsFirst = true;
        private bool isHaveCondtion = true;
        #region 构造函数

        /// <summary>
        /// 构造函数
        /// </summary>
        public FilterConditionSpecifySubClassStament(SqlBuildingContext context)
            : base()
        {
            this.context = context;
            this.isHaveCondtion = IsHaveCondition();
            base.CreateChildCollection();
        }

        #endregion
        private FilterConditionStatement mainSearchPathConditions = null;

        public FilterConditionStatement MainSearchPathConditions
        {
            get
            {
                if (!isHaveCondtion)
                    return null;
                return ModelTreeSQLBuilder.MainSearchPathConditions;
            }
        }

        private ModelTreeSQLBuilder modelTreeSQLBuilder = null;
        private ModelTreeSQLBuilder ModelTreeSQLBuilder
        {
            get
            {
                if (modelTreeSQLBuilder == null)
                {
                    var CompositeDataObject = context.CommonObject;
                    var queryFilter = context.QueryFilter;
                    var ContextBuilder = new ModelTreeContextBuilder(CompositeDataObject, queryFilter)
                    {
                        Entry = context.Node,
                    };

                    var GroupByNAggerateContext = ContextBuilder.Build();
                    modelTreeSQLBuilder = ModelTreeSQLBuilderFactory.GetBuilder(GroupByNAggerateContext);
                }
                return modelTreeSQLBuilder;
            }
        }

        #region 方法

        /// <summary>
        /// 生成exists条件查询SQL
        /// </summary>
        /// <returns>SQL语句</returns>
        public override string ToSQL()
        {
            if (!isHaveCondtion)
                return string.Empty;
            var sql = ModelTreeSQLBuilder.BuildSQL();
            //生成分页查询SQL时，这里可能会多执行一次
            if (IsFirst)
            {

                if (ModelTreeSQLBuilder.Condition?.ChildCollection != null)
                {
                    //生成条件子节点，DatabaseImpl 解析入参时用
                    foreach (ConditionStatement item in ModelTreeSQLBuilder.Condition.ChildCollection)
                    {
                        this.ChildCollection.Add(item);
                    }
                }
            }
            IsFirst = false;
            return sql;
        }

        private bool IsHaveCondition()
        {
            if (context.QueryFilter == null)
                return false;
            if ((context.QueryFilter.FilterClauses != null && context.QueryFilter.FilterClauses.Count > 0)
                || (context.QueryFilter.AggregateClause != null && context.QueryFilter.AggregateClause.Count > 0)
                || (context.QueryFilter.GroupByClause != null && context.QueryFilter.GroupByClause.Count > 0)
                    )
                return true;
            else
                return false;
        }

        //private bool IsEqual(ConditionStatement condition, ConditionStatement condition2)
        //{
        //    return
        //        condition.ConditionField.FieldName == condition2.ConditionField.FieldName
        //        && condition.ConditionField.Table?.TableName == condition2.ConditionField.Table?.TableName
        //        && condition.RelationalOperator == condition2.RelationalOperator
        //        && condition.LogicalOperator == condition2.LogicalOperator
        //        && condition.IsExpression == condition2.IsExpression
        //        && condition.DbType == condition2.DbType
        //        && condition.ConditionFieldValue.ConditionFieldName == condition2.ConditionFieldValue.ConditionFieldName
        //        && condition.ConditionFieldValue.DataType == condition2.ConditionFieldValue.DataType
        //        && condition.ConditionFieldValue.IsNull == condition2.ConditionFieldValue.IsNull
        //        && condition.ConditionFieldValue.Value == condition2.ConditionFieldValue.Value
        //        && condition.ConditionFieldValue.ValueType == condition2.ConditionFieldValue.ValueType;
        //}

        #endregion

    }
}
